package com.lwei.tallybook.client.action;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwei.tallybook.base.domain.AccountBookBill;
import com.lwei.tallybook.base.eums.ConstantEnum;
import com.lwei.tallybook.client.controller.BaseController;
import com.lwei.tallybook.service.service.AccountBookBillService;
import com.lwei.tallybook.service.vo.response.BillResponseVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 账单控制器
 * @author sks.lwei
 */
@Controller
@RequestMapping("/bill")
public class BillAction extends BaseController {

    @Resource
    private AccountBookBillService accountBookBillService;

    /**
     * 跳转账单查询界面
     */
    @RequestMapping("/to-search")
    public String toSearchBill(Model model){
        Date now = new Date();
        Date beginDayOfWeek = DateUtil.beginOfWeek(now);
        Date endDayOfWeek = DateUtil.endOfWeek(now);
        model.addAttribute("begin", beginDayOfWeek);
        model.addAttribute("end", endDayOfWeek);
        return "client/bill/bill-search";
    }

    /**
     * 日常账单查询
     * 即时生成
     * 默认使用当前日期
     */
    @RequestMapping("/show-daily-bill")
    public String showDailyBill(Model model){
        BillResponseVO accountBookBillDaily = accountBookBillService.showAccountBookBillDaily();
        model.addAttribute("bills", accountBookBillDaily);
        return "client/bill/bill_daily_detail";
    }

    /**
     * 账单查询
     * 数据库查询
     * 带时间范围
     */
    @RequestMapping("/show-search")
    public String showSearchBill(Model model, Date begin, Date end){
        BillResponseVO accountBookBillDaily = accountBookBillService.getAccountBookBillAll(begin, end);
        model.addAttribute("bills", accountBookBillDaily);
        return "client/bill/bill_detail";
    }

    /**
     * 展示账单列表
     * @param model model
     * @param page 分页
     * @param type 类型
     * @param close 是否结算
     * @return AccountBookBill
     */
    @GetMapping("/show-list")
    public String showBillList(Model model, Page<AccountBookBill> page, @RequestParam(value = "type", defaultValue = "auto") String type,
                               @RequestParam(value = "close", defaultValue = "false") boolean close){

        QueryWrapper<AccountBookBill> wrapper = new QueryWrapper<>();
        wrapper.eq("bill_type", type);
        wrapper.eq("close", close);
        wrapper.orderByDesc("create_time");
        IPage<AccountBookBill> billPage = accountBookBillService.pageBillByType(page, wrapper);

        model.addAttribute("billPage", billPage);
        return "client/bill/bill-list";
    }

    /**
     * 获取账单详情
     *
     * @param uuid  uuid
     * @return {@link String}
     */
    @GetMapping("/show-detail/{uuid}")
    public String showBillDetail(Model model, @PathVariable("uuid") String uuid){
        BillResponseVO accountBookBill = accountBookBillService.getAccountBookBill(uuid);
        model.addAttribute("bills", accountBookBill);
        return "client/bill/bill_detail";
    }

    /**
     * 账单详情
     */
//    @RequestMapping("/detail-book")
//    public String showBook(Model model, String uuid){
//        AccountBook accountBook = accountBookService.getAccountBookByUuid(uuid);
//        model.addAttribute("book", accountBook);
//        return "client/bill/bill_show";
//    }
}
